#include<bits/stdc++.h>
#define int long long
using namespace std;

int n,m,tot,a[100005],b[200005],id[200005],c[100005],res[100005];
string d[105];
void check(int len) {
    for(int i=1;i<=len;i++) id[i]=i;
    int dep=1,dl=0;
    int lastlen=len;
    for(int i=1;i<len;i+=2) {
        if(i>lastlen) {
            lastlen=len;
            dep++;
            dl=0;
        }
        if(d[dep][dl]=='0') {
            if(b[i]>=dep) {
                len++;
                b[len]=b[i];
                id[len]=id[i];
            } else {
                len++;
                b[len]=b[i+1];
                id[len]=id[i+1];
            }
        } else {
            if(b[i+1]>=dep) {
                len++;
                b[len]=b[i+1];
                id[len]=id[i+1];
            } else {
                len++;
                b[len]=b[i];
                id[len]=id[i];
            }
        }
        dl++;
    }
    res[id[len]]=1;
}
void dfs(int l,int r) {
    if(l>r) {
        check(r);
        return;
    }
    for(int i=0;i<=tot+1;i++) {
        b[l]=i;
        dfs(l+1,r);
    }
}
int solve(int k) {
    int len=k;
    while(len!=(len&-len)) len+=(len&-len);
    for(int i=1;i<=len;i++) res[i]=0;
    for(int i=1;i<=k;i++) b[i]=a[i];
    dfs(k+1,len);
    int sum=0;
    for(int i=1;i<=len;i++) sum+=res[i]*i;
    return sum;
}
signed main() {
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=m;i++) cin>>c[i];
    while(true) {
        tot++;
        cin>>d[tot];
        if(d[tot].size()==1) break;
    }
    int q;
    cin>>q;
    for(int i=1;i<=q;i++) {
        int x0,x1,x2,x3;
        cin>>x0>>x1>>x2>>x3;
        for(int j=1;j<=n;j++) {
            if(j%4==0) a[j]^=x0;
            if(j%4==1) a[j]^=x1;
            if(j%4==2) a[j]^=x2;
            if(j%4==3) a[j]^=x3;
        }
        int ans=0;
        for(int j=1;j<=m;j++) {
            ans^=(1ll*j*solve(c[j]));
        }
        cout<<ans<<"\n";
        for(int j=1;j<=n;j++) {
            if(j%4==0) a[j]^=x0;
            if(j%4==1) a[j]^=x1;
            if(j%4==2) a[j]^=x2;
            if(j%4==3) a[j]^=x3;
        }
    } 
    return 0;
}